<body>
<script src="../fullscreen/full-screen-test.js"></script>
<span></span>
<script>
    var span = document.getElementsByTagName('span')[0];

    var gamepadConnected = function() {
        navigator.getGamepads();
        document.documentElement.requestFullscreen();
        waitForEvent(document, 'fullscreenchange', documentEnteredFullscreen, false, true);
    }

    var documentEnteredFullscreen = function() {
        testExpected("document.fullscreenElement", document.documentElement);
        span.requestFullscreen();
        waitForEvent(document, 'fullscreenchange', spanEnteredFullscreen, false, true);
    };

    var spanEnteredFullscreen = function() {
        testExpected("document.fullscreenElement", span);
        waitForEvent(document, 'fullscreenchange', spanExited, false, true);
        document.exitFullscreen();
    };

    var spanExited = function() {
        testExpected("document.fullscreenElement", document.documentElement);
        waitForEvent(document, 'fullscreenchange', documentExited, false, true);
        document.exitFullscreen();
    };

    var documentExited = function() {
        testExpected("document.fullscreenElement", undefined);
        endTest();
    };

    waitForEvent(window, 'gamepadconnected', gamepadConnected, false, true);
    if (window.gamepadController) {
        // Simulate pressing a button on a connected gamepad. A gamepad
        // button press should be treated as a user activation for the
        // fullscreen API.
        gamepadController.connect(0);
        gamepadController.setId(0, "FullScream Maximizer");
        gamepadController.setButtonCount(0, 1);
        gamepadController.setButtonData(0, 0, 1);
        gamepadController.dispatchConnected(0);
    }
</script>
